package leetcode;

import list.ListNode;

public class RomoveNthNodeFromEnd {

	public ListNode removeNthFromEnd(ListNode head, int n) {
	        if(head == null){
	            return head;
	        }
	        ListNode temp = head;
	        ListNode fast = head, slow = head;
	        int length = 1;
	        //first we should know the list length
	        while(temp.next != null){
	            length++;
	            temp = temp.next;
	        }
	        if(length < n){
	            throw new IllegalArgumentException("the list does't have n node");
	        }
	        //如果要删除的是头结点，就直接返回一个结点
	        if(length == n){
	            return head.next;
	        }
	        int i = 1;
	        temp = head;
	        while(i < length - n){
	            temp = temp.next;
	            i++;
	        }
	        temp.next = temp.next.next;
	        return head;
	    }
}
